package jsyf.oa.dao.wreport;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import jsyf.com.dao.BaseJdbcDao;
import jsyf.oa.entity.wreport.WReportMainEntity;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/11/22 0022.
 */
@Repository
public class WReportMainDao extends BaseJdbcDao {

    public Integer add(WReportMainEntity model) {
        model.setId(super.generateKey());
        model.setCreateTime(super.getCurrentTime());
        return super.insert("T_WRT_MAIN", model.getJSONObject());
    }

    public Integer update(WReportMainEntity model) {
        return super.update("T_WRT_MAIN", model.getJSONObject(), model.getJSONObjectId());
    }

    //物理删除
    public Integer remove(String id) {
        String sql = "delete from T_WRT_MAIN where ID = ?";
        String[] params = {id};
        return super.delete(sql, params);
    }

    public Integer removeByUserIdAndReportDate(String userId, String reportDate) {
        String sql = "delete from T_WRT_MAIN where USER_ID = ? AND REPORT_DATE = ?";
        String[] params = {userId, reportDate};
        return super.delete(sql, params);
    }
    public List<JSONObject> getDayCharge(String userId, String reportDate){
        StringBuffer sql=new StringBuffer();
        sql.append("SELECT");
        sql.append(" T_WRT_COST.ID AS id,");
        sql.append(" T_WRT_COST.W_ID AS wid,");
        sql.append(" T_WRT_COST.COST_BIG_ID AS costBigId,");
        sql.append(" T_WRT_COST.COST_BIG_NAME AS costBigName,");
        sql.append(" T_WRT_COST.COST_SMALL_ID AS costSmallId,");
        sql.append(" T_WRT_COST.COST_SMALL_NAME AS costSmallName,");
        sql.append(" T_WRT_COST.MONEY AS money,");
        sql.append(" T_WRT_COST.INVOICE_FLAG AS invoiceFlag,");
        sql.append(" T_WRT_COST. START AS start,");
        sql.append(" T_WRT_COST.REMARK AS remark,");
        sql.append(" T_WRT_COST.AUDIT_FLAG AS auditFlag,");
        sql.append(" T_WRT_COST_DETAIL.IMG AS img");
        sql.append(" FROM");
        sql.append(" T_WRT_COST");
        sql.append(" LEFT JOIN T_WRT_COST_DETAIL ON T_WRT_COST.ID = T_WRT_COST_DETAIL.FK_COST_ID");
        sql.append(" WHERE");
        sql.append(" T_WRT_COST.CREATE_USER_ID = ?");
        sql.append(" AND");
        sql.append(" LEFT (T_WRT_COST.REPORT_DATE, 10) = ?");
        List<JSONObject> jsonList = super.queryForJsonList(sql.toString(),new Object[]{userId,reportDate});
        return  jsonList;
    }
    public List<JSONObject> getByDetail(String costBigId) {
        StringBuffer sql = new StringBuffer();
        sql.append(" SELECT \n");
        sql.append(" ID AS id,");
        sql.append(" NAME AS name \n");
        sql.append(" FROM \n");
        sql.append(" t_wrt_expenses \n");
        // 查询条件
        sql.append(" WHERE PARENT_ID = ? \n");
        Object[] params = {costBigId};
        // 查询结果
        return super.queryForJsonList(sql.toString(), params);
    }

    public Integer del(WReportMainEntity model) {
        return 0;
    }


    public WReportMainEntity getById(String id){
        String condition = " AND ID = ? ";
        String[] params = {id};
        List<WReportMainEntity> list = this.getByCondition(condition,params);
        if ( list != null && list.size() > 0 ){
            return list.get(0);
        } else {
            return null;
        }
    }

    public List<WReportMainEntity> getByCondition(String condition,Object[] params){
        return this.getByPage(condition,params,1,Integer.MAX_VALUE);
    }

    public List<WReportMainEntity> getByPage(String condition,Object[] params, int requestPage, int pageSize){
        StringBuffer sql = new StringBuffer();
        sql.append(" SELECT \n");
        sql.append(" ID AS id, \n");
        sql.append(" TITLE AS title, \n");
        sql.append(" CREATE_TIME AS createTime, \n");
        sql.append(" USER_ID AS userId, \n");
        sql.append(" REPORT_DATE AS reportDate, \n");
        sql.append(" MODULE_E_NAMES AS moduleENames, \n");
        sql.append(" FLAGE AS flage \n");
        sql.append(" FROM T_WRT_MAIN \n");
        sql.append(" WHERE 1 = 1 \n");
        sql.append(condition);

        super.appendPageSql(sql,requestPage,pageSize);

        List<JSONObject> jsonList = super.queryForJsonList(sql.toString(),params);

        List<WReportMainEntity> list = new ArrayList<>();

        for (int i = 0; i < jsonList.size(); i++) {
            WReportMainEntity model = JSON.toJavaObject(jsonList.get(i),WReportMainEntity.class);
            list.add(model);
        }
        return list;
    }


    public int getCount(String condition,Object[] params){
        StringBuffer sql = new StringBuffer();
        sql.append(" SELECT \n");
        sql.append(" COUNT(1) \n");
        sql.append(" FROM T_WRT_MAIN \n");
        sql.append(" WHERE 1=1 \n");
        sql.append(condition);
        String count = super.queryForString(sql.toString(),params);
        return Integer.parseInt(count);
    }

    public WReportMainEntity getByUserIdAndReportDate(String userId, String reportDate){
        List<WReportMainEntity> list = getByCondition(" and USER_ID = ? and REPORT_DATE = ?",
                new String[]{userId,reportDate});
        if(list != null && list.size()>0){
            return list.get(0);
        }else{
            return null;
        }
    }


}

